﻿using System;
using System.Data.SqlClient;
using System.Reflection;

namespace ProgramMain.Framework
{
    public class TimeoutTableAdapter : System.ComponentModel.Component
    {
        public int InsertCommandTimeout
        {
            get
            {
                SqlDataAdapter Adapter = SqlAdapter;
                return ((Adapter != null && Adapter.InsertCommand != null) ? Adapter.InsertCommand.CommandTimeout : 30);
            }

            set
            {
                SqlDataAdapter Adapter = SqlAdapter;
                if (Adapter != null && Adapter.InsertCommand != null)
                    Adapter.InsertCommand.CommandTimeout = value;
            }
        }

        public int UpdateCommandTimeout
        {
            get
            {
                SqlDataAdapter Adapter = SqlAdapter;
                return ((Adapter != null && Adapter.UpdateCommand != null) ? Adapter.UpdateCommand.CommandTimeout : 30);
            }

            set
            {
                SqlDataAdapter Adapter = SqlAdapter;
                if (Adapter != null && Adapter.UpdateCommand != null)
                    Adapter.UpdateCommand.CommandTimeout = value;
            }
        }

        public int DeleteCommandTimeout
        {
            get
            {
                SqlDataAdapter Adapter = SqlAdapter;
                return ((Adapter != null && Adapter.DeleteCommand != null) ? Adapter.DeleteCommand.CommandTimeout : 30);
            }

            set
            {
                SqlDataAdapter Adapter = SqlAdapter;
                if (Adapter != null && Adapter.DeleteCommand != null)
                    Adapter.DeleteCommand.CommandTimeout = value;
            }
        }

        public int SetUpdateTimeout
        {
            set
            {
                SqlDataAdapter Adapter = SqlAdapter;
                if (Adapter != null)
                {
                    if (Adapter.InsertCommand != null)
                        Adapter.InsertCommand.CommandTimeout = value;
                    if (Adapter.UpdateCommand != null)
                        Adapter.UpdateCommand.CommandTimeout = value;
                    if (Adapter.DeleteCommand != null)
                        Adapter.DeleteCommand.CommandTimeout = value;
                }
            }
        }

        public int SelectCommandTimeout
        {
            get
            {
                SqlCommand[] SqlSelect = SqlSelectCommand;
                return ((SqlSelect.Length > 0) ? SqlSelect[0].CommandTimeout : 30);
            }

            set
            {
                SqlCommand[] SqlSelect = SqlSelectCommand;
                for (int i = 0; i < SqlSelect.Length; i++)
                {
                    if ((SqlSelect[i] != null))
                    {
                        SqlSelect[i].CommandTimeout = value;
                    }
                }
            }
        }

        protected SqlDataAdapter SqlAdapter
        {
            get
            {
                SqlDataAdapter Adapter = null;
                Type type = this.GetType();
                PropertyInfo pi = type.GetProperty("Adapter", BindingFlags.NonPublic | BindingFlags.Instance);
                if (pi != null)
                    Adapter = (SqlDataAdapter)pi.GetValue(this, null);
                return Adapter;
            }
        }

        protected SqlCommand[] SqlSelectCommand
        {
            get
            {
                SqlCommand[] CommandCollection = new SqlCommand[] { };
                Type type = this.GetType();
                PropertyInfo pi = type.GetProperty("CommandCollection", BindingFlags.NonPublic | BindingFlags.Instance);
                if (pi != null)
                {
                    SqlCommand[] res = (SqlCommand[])pi.GetValue(this, null);
                    if (res != null)
                        CommandCollection = res;
                }
                return CommandCollection;
            }

        }
    }
}
